from exts import db
from datetime import datetime

class EventModel(db.Model):
    __tablename__ = 'event'
    # id
    event_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 标题
    title = db.Column(db.String(50), nullable=False)
    # 方面缩略图url
    cover_img = db.Column(db.String(100), nullable=False,  default="http://127.0.0.1:5000/images/event/default.jpeg")
    # 活动详情
    details = db.Column(db.Text, nullable=False)
    # 开始时间
    begin_time = db.Column(db.DateTime, nullable=False)
    # 结束时间
    finish_time = db.Column(db.DateTime, nullable=False)
    # 地址
    location = db.Column(db.String(200), nullable=False)

    def to_dict(self):
        return {c.name: self._convert_value(getattr(self, c.name, None)) for c in self.__table__.columns}

    @staticmethod
    def _convert_value(value):
        if isinstance(value, datetime):
            return int(value.timestamp() * 1000)  # 转换为毫秒级时间戳
        return value
